package com.ykzb.image.controller;

import com.ykzb.constants.Constants;
import com.ykzb.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/image/")
public class ImageController {



	/**
	 * 生成二维码
	 * @return
	 */
	@RequestMapping(value = "/getVerifyCode")
	public void getVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		//生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		//存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute(Constants.VERIFIY_SESSIOON_CODE, verifyCode.toLowerCase());
		//生成图片
		int w = 120, h = 34;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}


}
